﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using IntfClass;

namespace IntfSetting
{
    public partial class FrmSave : Form
    {
        public FrmSave()
        {
            InitializeComponent();
        }

        public Boolean blnNew = true;
        public Int64 ID = 0;
        public string strCode = string.Empty;
        public string strName = string.Empty;
        public DataTable dtData = new DataTable();


        private void btnSave_Click(object sender, EventArgs e)
        {
            ClassDB classDB = new ClassDB();

            int intKind = 0;
            if (rad1.Checked)
            {
                intKind = 1;
                if (this.txtFilePath.Text.Equals(string.Empty))
                {
                    MessageBox.Show("路径不可为空");
                    this.txtFilePath.Focus();
                    return;
                }
            }

            if (rad2.Checked)
            {
                intKind = 2;

                //if (this.txtFtpPath.Text.Equals(string.Empty))
                //{
                //    MessageBox.Show("路径不可为空");
                //    this.txtFtpPath.Focus();
                //    return;
                //}

                if (this.txtURL.Text.Equals(string.Empty))
                {
                    MessageBox.Show("URL不可为空");
                    this.txtURL.Focus();
                    return;
                }

                //if (this.txtUser.Text.Equals(string.Empty))
                //{
                //    MessageBox.Show("用户不可为空");
                //    this.txtUser.Focus();
                //    return;
                //}

                //if (this.txtPwd.Text.Equals(string.Empty))
                //{
                //    MessageBox.Show("密码不可为空");
                //    this.txtPwd.Focus();
                //    return;
                //}
            }

            if (dtData.Rows.Count == 0)
            {
                DataRow dataRow = dtData.NewRow();
                dataRow["Kind"] = intKind;
                dataRow["FilePath"] = this.txtFilePath.Text;
                dataRow["URL"] = this.txtURL.Text;
                dataRow["FTPPath"] = this.txtFtpPath.Text;
                dataRow["FtpUser"] = this.txtUser.Text;
                dataRow["ftpPWD"] = this.txtPwd.Text;

                dtData.Rows.Add(dataRow);
                classDB.ExecuteBulkInsert("TSave", dtData);

            }
            else
            {
                StringBuilder strSql = new StringBuilder();
                strSql.AppendLine("Update TSave");
                strSql.AppendLine("Set Kind = @Kind");
                strSql.AppendLine(",FilePath = @FilePath");
                strSql.AppendLine(",URL = @URL");
                strSql.AppendLine(",FTPPath = @FTPPath");
                strSql.AppendLine(",FtpUser = @FtpUser");
                strSql.AppendLine(",ftpPWD = @ftpPWD");
                strSql.AppendLine("Where ID = @ID");


                SqlParameter sqlParameter1 = new SqlParameter("Kind",SqlDbType.SmallInt);
                sqlParameter1.Value = intKind;
                SqlParameter sqlParameter2 = new SqlParameter("FilePath", SqlDbType.NVarChar);
                sqlParameter2.Value = this.txtFilePath.Text.Trim();
                SqlParameter sqlParameter3 = new SqlParameter("URL", SqlDbType.NVarChar);
                sqlParameter3.Value = this.txtURL.Text.Trim();
                SqlParameter sqlParameter4 = new SqlParameter("FTPPath", SqlDbType.NVarChar);
                sqlParameter4.Value = this.txtFtpPath.Text.Trim();
                SqlParameter sqlParameter5 = new SqlParameter("FtpUser", SqlDbType.NVarChar);
                sqlParameter5.Value = this.txtUser.Text.Trim();
                SqlParameter sqlParameter6 = new SqlParameter("ftpPWD", SqlDbType.NVarChar);
                sqlParameter6.Value = this.txtPwd.Text;
                SqlParameter sqlParameter7 = new SqlParameter("ID", SqlDbType.Int);
                sqlParameter7.Value = dtData.Rows[0]["ID"];


                classDB.ExecuteNone(strSql.ToString(),
                    new SqlParameter[] { sqlParameter1, sqlParameter2, sqlParameter3, sqlParameter4, sqlParameter5, sqlParameter6, sqlParameter7 });
            }

            try
            {
                MessageBox.Show("保存成功！");


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void FrmBuinessNew_Load(object sender, EventArgs e)
        {
            SearchDate();
            if (dtData.Rows.Count > 0)
            {
                if (dtData.Rows[0]["Kind"].ToString().Equals("1"))
                {
                    this.rad1.Checked = true;
                    this.panel1.Enabled = true;
                    this.panel2.Enabled = false;
                }
                else
                {
                    this.rad2.Checked = true;
                    this.panel1.Enabled = false;
                    this.panel2.Enabled = true;
                }

                this.txtFilePath.Text = dtData.Rows[0]["FilePath"].ToString();
                this.txtURL.Text = dtData.Rows[0]["URL"].ToString();
                this.txtFtpPath.Text = dtData.Rows[0]["FTPPath"].ToString();
                this.txtUser.Text = dtData.Rows[0]["FtpUser"].ToString();
                this.txtPwd.Text = dtData.Rows[0]["ftpPWD"].ToString();

            }
            else
            {
                this.rad1.Checked = true;
            }

        }

        private void SearchDate()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("select * from TSave t");

            ClassDB classDB = new ClassDB();
            dtData = classDB.ExecuteDataTable(strSql.ToString());

        }

        private void rad1_CheckedChanged(object sender, EventArgs e)
        {
            if (rad1.Checked)
            {
                panel2.Enabled = false;
                panel1.Enabled = true;
            }
            else
            {
                panel1.Enabled = false;
                panel2.Enabled = true;
            }
        }


    }
}
